import router from '@/router'
import {post} from '@/util/request'

export default {
	namespace:'spitslot',
	namespaced:true,
	state:{
		isLoading:false,
		isModalLoading:false,
		pageData: {},
		detail: {},
		authList: []
	},
	mutations:{
		changeLoading(state,v){
			state.isLoading = v;
		},
		changeModalLoading(state,v){
			state.isModalLoading = v;
		},
		changePagedata(state,v){
			state.pageData = v;
		}
	},
	actions:{
		async reload({commit},data){
			commit('changeLoading',true);
			let res = await post('/spitslot/list', data);
			res.data.data.thisPageElements.forEach((v,i) => {
				let speechContentStr = v.speechContent.length>15?v.speechContent.substring(0,15)+'...':v.speechContent;
        v.speechContentStr = speechContentStr;
      });
			commit('changePagedata',res.data.data);
			commit('changeLoading',false);
		},
		deleteSpitslot({commit},id){
			commit('changeModalLoading',true);
			return post('/spitslot/delete',{id}).then(json => {
				commit('changeModalLoading',false);
				return json.data;
			});
		}
	},
	subscriptions({dispatch},router){
		//给路由加监听一旦进入module绑定的页面时dispatch初始化action
		router.afterEach((to,from)=>{
			if(to.name === this.namespace){
				dispatch('reload',{})
			}
		})
	}
}
